package Cliente.ventanas;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JLayeredPane;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

import Cliente.TCPCliente;

/**
 * Ventana en la que se introduce el nick del usuario para validar
 * 
 * @author Jon
 *
 */

public class IntroWindow extends JFrame implements ActionListener{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	//comandos
	private final static String userOk="210 OK Bienvenido.";
	private final static String passwordOk="211 OK Bienvenido al sistema.";
	
	private JLabel textLabelUp;
	private JLabel textLabelDown;
	private JTextField fieldTextUp;
	private JTextField fieldTextDown;
	private JPasswordField pass;
	private JButton OK;
	private int fase;
	TCPCliente tcp;
	
	public IntroWindow(){
		
		JLabel img = new JLabel(new ImageIcon("src/Img/fondopeq.jpg"));
		
		this.setTitle("InfoTren V0.1");
		this.setResizable(false);
		this.setSize(300,150);
		this.setVisible(true);
		this.setLayout(null);
		this.setDefaultCloseOperation(EXIT_ON_CLOSE);
		this.setLocationRelativeTo(null);
		fase = 0;
		pass = new JPasswordField();
		OK = new JButton("OK");
		textLabelUp = new JLabel("Introduza IP :");
		fieldTextUp = new JTextField();
		textLabelDown = new JLabel("Introduza Puerto :");
		fieldTextDown = new JTextField();
		

		textLabelUp.setForeground(Color.white);

		textLabelDown.setForeground(Color.white);
		
		pass.setEnabled(false);
		pass.setVisible(false);
		
		JLayeredPane layer = new JLayeredPane();
		
		layer.setLayout(null);
		
		this.add(layer);
		layer.add(img,new Integer(0));
		layer.add(OK, new Integer(2));
		layer.add(textLabelUp, new Integer(1));
		layer.add(fieldTextUp, new Integer(1));
		layer.add(textLabelDown, new Integer(1));
		layer.add(fieldTextDown, new Integer(1));
		layer.add(pass, new Integer(1));

		
		layer.setBounds(0,0,300,150);
		img.setBounds(0,0,300,150);
		textLabelUp.setBounds(20,20,100,20);
		fieldTextUp.setBounds(140,20,100,20);
		pass.setBounds(140, 20, 100, 20);
		textLabelDown.setBounds(20,60,100,20);
		fieldTextDown.setBounds(140,60,50,20);
		OK.setBounds(180,85,100,25);
		
		fieldTextUp.setText("127.0.0.1");
		fieldTextDown.setText("6968");
		OK.addActionListener(this);
	}
	


	public void USER(String nombre){
	
	}
	@SuppressWarnings("deprecation")
	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
//		System.out.println("fase1");
		if(fase==1){
			
			try {
				tcp.sentMessage("USER " + fieldTextUp.getText()+"\n");
				String comandoRecibido=tcp.receiveMessage();
				if(comandoRecibido.equals(userOk)){
					fase++;
					textLabelUp.setText("Introduce Password :");
					fieldTextUp.setEnabled(false);
					fieldTextUp.setVisible(false);
					pass.setEnabled(true);
					pass.setVisible(true);
					pass.setText("password1");
				}
				
			} catch (IOException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			
		}else{
			if(fase==0){
				try {
					tcp = new TCPCliente(fieldTextUp.getText(), Integer.parseInt(fieldTextDown.getText()));
					fase++;
					fieldTextDown.setEnabled(false);
					fieldTextDown.setVisible(false);
					textLabelDown.setVisible(false);
					fieldTextDown.setEnabled(false);
					textLabelUp.setText("Introduce Nick :");
					fieldTextUp.setText("user1");
				} catch (NumberFormatException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
					
				} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				
			}else
				if(fase==2){
					try {
						String enviar="PASS " + pass.getText()+"\n";
						tcp.sentMessage(enviar);
						String recibido=tcp.receiveMessage();
						if(recibido.equals(passwordOk)){
							this.dispose();
							new MainWindow (tcp);
							
						}
						
					} catch (IOException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
					
				}
		}
	}
	
	public static void main(String[] args){
		new IntroWindow();
	}

}
